﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_Public;
using QLTV_BLL;

namespace GUI
{
    public partial class frmDocGia : DevExpress.XtraEditors.XtraForm
    {
        #region Khai bao bien
        DocGia_BLL dg_bll = new DocGia_BLL();
        TextMessageBox box = new TextMessageBox();
        public string maDocGia="";
        private string maLoaiThe = "";
        DataTable dt;
        private  string checkbox;
        private bool check;
        #endregion

        #region Loadform

        public frmDocGia()
        {
            InitializeComponent();
        }

        private void frmDocGia_Load(object sender, EventArgs e)
        {
            LoadData();
            LoadCombobox();
        }
        #endregion

        #region cac chuc nang

        private void btnLuuThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (checkValid())
            {
                DocGia_Public dg = new DocGia_Public();
                if (maDocGia == "")
                {
                    if (checkMaInsert())
                    {
                        dg.MaDocGia = txtMaDocGia.Text.Trim();
                        dg.NgayLamThe = dtpNgayLamThe.Value;
                        dg.NgayHetHan = dtpNgayHetHan.Value;
                        dg.MaLoaiThe = maLoaiThe;
                        dg.TrangThaiSD = check;
                        dg.MaNhanVien = box.MaNhanVien;

                        dg_bll.Insert(dg);
                        DialogResult = DialogResult.OK;
                    }
                }
                else
                {
                    dg.MaDocGia = txtMaDocGia.Text.Trim();
                    dg.NgayLamThe = dtpNgayLamThe.Value;
                    dg.NgayHetHan = dtpNgayHetHan.Value;
                    dg.MaLoaiThe = maLoaiThe;
                    dg.TrangThaiSD = check;
                    dg.MaNhanVien = box.MaNhanVien;

                    dg_bll.Insert(dg);
                    DialogResult = DialogResult.OK;
                }
            }
        }

        private void btnLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (checkValid())
            {
                DocGia_Public dg = new DocGia_Public();
                if (maDocGia == "")
                {
                    if (checkMaInsert())
                    {
                        dg.MaDocGia = txtMaDocGia.Text.Trim();
                        
                        dg.NgayLamThe = dtpNgayLamThe.Value;
                        dg.NgayHetHan = dtpNgayHetHan.Value;
                        dg.MaLoaiThe = maLoaiThe;
                        dg.TrangThaiSD = check;
                        dg.MaNhanVien = box.MaNhanVien;

                        dg_bll.Insert(dg);
                        btnHuy_ItemClick(sender,e);
                    }
                }
                else
                {
                    dg.MaDocGia = txtMaDocGia.Text.Trim();
                    
                    dg.NgayLamThe = dtpNgayLamThe.Value;
                    dg.NgayHetHan = dtpNgayHetHan.Value;
                    dg.MaLoaiThe = maLoaiThe;
                    dg.TrangThaiSD = check;
                    dg.MaNhanVien = box.MaNhanVien;

                    dg_bll.Insert(dg);
                    btnHuy_ItemClick(sender, e);
                }
            }
        }

        private void btnHuy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            box.Huy_Text(this);
            cboMaLoaiThe.Text = "";
            ckbTrangThai.CheckState = CheckState.Checked;
            dtpNgayLamThe.Refresh();
            dtpNgayHetHan.Refresh();
            maDocGia = "";
            maLoaiThe = "";
            txtMaDocGia.Enabled = true;
        }

        private void btnDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        #endregion

        #region Cac su kien

        private void frmDocGia_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.Cancel == false)
                DialogResult = DialogResult.OK;
        }

        private void cboMaLoaiThe_TextChanged(object sender, EventArgs e)
        {
            try
            {
                maLoaiThe = cboMaLoaiThe.EditValue.ToString();
            }
            catch (Exception)
            { }
        }

        private void ckbTrangThai_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbTrangThai.CheckState == CheckState.Checked)
                check = true;
            else 
                check = false;
        }
        #endregion

        #region Ham viet them
        private void LoadData()
        {
            if (maDocGia != "")
            {
                dt = dg_bll.TaoBang(box.sqlCheckExist("DocGia", "MaDocGia", maDocGia));
                DataRow dr = dt.Rows[0];
                txtMaDocGia.Text = dr["MaDocGia"].ToString();
                txtSoThe.Text = dr["SoThe"].ToString();
                dtpNgayLamThe.Text = dr["NgayLamThe"].ToString();
                dtpNgayHetHan.Text = dr["NgayHetHan"].ToString();
                maLoaiThe=dr["MaLoaiThe"].ToString();
                checkbox=dr["TrangThaiSD"].ToString();
                if (checkbox == "True")
                {
                    ckbTrangThai.CheckState = CheckState.Checked;
                }
                else
                    ckbTrangThai.CheckState = CheckState.Unchecked;
                txtMaDocGia.Enabled = false;
            }
        }
        private bool checkMaInsert()
        {
            dt = dg_bll.TaoBang(box.sqlCheckExist("DocGia","MaDocGia",txtMaDocGia.Text.Trim()));
            if (dt.Rows.Count > 0)
            {
                XtraMessageBox.Show(box.Exist(txtMaDocGia.Text),box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
                txtMaDocGia.Focus();
                return false;
            }
            return true;
        }

        private bool checkValid()
        {
            if (txtMaDocGia.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Mã độc giả.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtMaDocGia.Focus();
                return false;
            }
            if (txtSoThe.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Mã số thẻ.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtSoThe.Focus();
                return false;
            }
            if(box.CompareDate(dtpNgayHetHan.Value,dtpNgayLamThe.Value)==1)
            {
                XtraMessageBox.Show("Ngày hết hạn phải lớp hơn Ngày làm thẻ.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
                dtpNgayHetHan.Focus();
                return false;
            }
            if(cboMaLoaiThe.Text.Trim()=="")
            {
                XtraMessageBox.Show("Bạn chưa chọn Loại thẻ.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                cboMaLoaiThe.Focus();
                return false;
            }
            return true;
        }
        private void LoadCombobox()
        {
            cboMaLoaiThe.Properties.DataSource = dg_bll.TaoBang(box.sqlSelectFile("MaLoaiThe","TenLoaiThe","DM_LoaiThe"));
            cboMaLoaiThe.Properties.DisplayMember = "TenLoaiThe";
            cboMaLoaiThe.Properties.ValueMember = "MaLoaiThe";
        }
        #endregion
    }
}